using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ACM.cmsarea08;
using ACM.cmsarea08.Controls;
using ACM.cmsarea08.Administration;
using ACM.cmsarea08.Administration.Controls;


namespace ACM.cmsarea08.Administration.Core {
	public partial class GesLingue :  BaseDGridPage {
		private const int offSet = 20;
		protected Itan_DataGrid insLingue;
		private ACM.cmsarea08.Administration.Core.DBClass dclass = new cmsarea08.Administration.Core.DBClass();
		private string[] DgridValue = new string[2];
		
		private int TotalePagine {
			get {
				if(ViewState["pagine"]==null)
					return -1;
				else
					return (int)ViewState["pagine"];
			}
			set {
				ViewState["pagine"]=value;
			}
		}
		private int PaginaCorrente {
			get {
				if(ViewState["PaginaCorrente"]==null)
					return -1;
				else
					return (int)ViewState["PaginaCorrente"];
			}
			set {
				ViewState["PaginaCorrente"]=value;
			}
		}
			
		
		override protected void OnInit(EventArgs e)
		{
			if(ACM.cmsarea08.Users.CurrentUser.Role.Score>400)
				Response.Redirect("/loginadm.aspx?returnurl="+
								HttpUtility.UrlEncode(Request.ServerVariables["SCRIPT_NAME"].ToString()+
				                                        (Request.ServerVariables["QUERY_STRING"].ToString()==""?"":"?"+Request.ServerVariables["QUERY_STRING"].ToString()))
	);
			else if(ACM.cmsarea08.Users.CurrentUser.Role.Score>100)
				Response.Redirect("/administration/accessdeny.aspx");
			InitializeComponent();
			base.OnInit(e);
		}

		private void InitializeComponent()
		{
			if(this.Master is ACM.cmsarea08.Administration.Core.ListerPrivate) {
//				((itareanet.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.EditMode=TipiPagineLista.BlocchiVisivi;
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.DBActionTable="lingue";
//				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.Custom1 = "<img src=\"/administration/template/img/p_predef.gif\" alt=\"set Predef\" />Imposta Predefinito";
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.ShowDelete=false;
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.ShowClone=false;
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.ShowAddNew=false;
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.ShowAddDefault=false;
//				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.Custom1_Click += new EventHandler(this.SettaPredef);
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.HelpPath="/administration/docs/GesLingue.html.html";
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.DataSourceChanged+= new EventHandler(RicaricaDati);
				//jmsdefault
				CustomButton cmb = new CustomButton("IMP_PREDEF","<img src=\"/administration/template/img/spacer.gif\" alt=\"set predef\" />Imposta predefinito","",new EventHandler(this.SettaPredef) );
				cmb.CssClass="jmsdefault";
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.setCustomButtons(	cmb );
				
				
				
			}
			
			this.Load += new System.EventHandler(this.Page_Load);
			insLingue.CambiaPagina += new Itan_DataGridEventHandler(CambiaPagina);
			insLingue.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(GrigliaCommand);
			insLingue.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(MentreCaricaDati);
		}
	
		private void Page_Load(object o , EventArgs e) {
			CaricaDati();
			insLingue.Title = rm.GetString("lang_list");
			if(!Page.IsPostBack) {
				
			}
		}
		private void CaricaDati() {
			if(PaginaCorrente==-1)
				PaginaCorrente = 0;
			
			if(ElencoGriglia==null) {
				insLingue.DataSource=null;
				ElencoGriglia = dclass.GetAdminLingue((PaginaCorrente*offSet),offSet);
			}
			if(ElencoGriglia==null) {
				insLingue.Visible=false;
				return;
			}
			if(ElencoGriglia.Tables[0].Columns.Contains("uconteggio") && 
			   ElencoGriglia.Tables[0].Rows.Count>0 ) {
				int totaleC =0;
				Int32.TryParse(ElencoGriglia.Tables[0].Rows[0]["uconteggio"].ToString(), out totaleC);
				TotalePagine = (totaleC/offSet) + (totaleC%offSet>1?1:0);
			}


			insLingue.PageNum=TotalePagine;
			insLingue.CurrentPage = PaginaCorrente;
			insLingue.Visible=true;
			if(insLingue.DataSource==null) {
				insLingue.DataSource = ElencoGriglia;
				insLingue.DataBind();
			}
		}
		private void CambiaPagina(object o, Itan_DataGridEventArgs e) {
			PaginaCorrente=e.NewPage;
			ElencoGriglia=null;
			CaricaDati();
		}
		private void SettaPredef(object o, EventArgs e) {
				
			
			//requisito TABELLA DI RIFERIMENTO
		if(((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.DBActionTable==string.Empty) {
			((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.ShowResultMessage(rm.GetString("error_set_default") + ", " + rm.GetString("error_dbactiontable"));		
			return;
		}
				
		//acquisisci: valore selezionato:
		if(CercaValueKey()) {
			//puoi continuare
			if(DgridValue[1]!=string.Empty) {
				//esegui il clone delete from 
				const string sampleupd ="update {0} set predef='0' where predef='1'";
				const string sampledef ="update {0} set predef='1' where {1}='{2}';";
				int i_reso = 0;
				i_reso += ACM.cmsarea08.DBManager.mainConnection.Helper_NonQuery(
					string.Format(sampleupd,ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle + ((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.DBActionTable));
				i_reso += ACM.cmsarea08.DBManager.mainConnection.Helper_NonQuery(
					string.Format(sampledef,ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle + ((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.DBActionTable,
				                                    DgridValue[0],DgridValue[1])
				);
				if(i_reso < 2) {
					((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.ShowResultMessage(rm.GetString("error_failed"));
				} else {
					RicaricaDati(new object(),new EventArgs());
					((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.ShowResultMessage(rm.GetString("success_set_default"));
					ACM.cmsarea08.mainconfiguration.MainConf.LinguaPredefinita = DgridValue[1];
				}
				
//				HttpContext.Current.Response.Write(string.Format(sampleupd,ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle + DBActionTable) +
//				                                   "<hr>"+ string.Format(sampledef,ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle + DBActionTable,
//				                                    DgridValue[0],DgridValue[1]) +"<hr>"+ex.ToString());
				
				
			} else
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.ShowResultMessage(rm.GetString("error_set_default") + ", " + rm.GetString("error_noselect"));
				
			
		} else
			((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.ShowResultMessage(rm.GetString("error_no_grid"));	
		}
		private bool CercaValueKey() {
		for(int i =0; i<this.Page.Controls.Count;i++) {
				CercaControllo(this.Page.Controls[i]);	
				if(DgridValue[0]!=string.Empty  || DgridValue[1]!=string.Empty) {
					break;
				}
		}
		if(DgridValue[0]!=string.Empty  || DgridValue[1]!=string.Empty)
			return true;
		else
			return false;
	}
	private void CercaControllo(Control ctrl) {
		if(ctrl is ACM.cmsarea08.Controls.Itan_DataGrid) {
				DgridValue[1] = ((ACM.cmsarea08.Controls.Itan_DataGrid)ctrl).SelectedRow;
				DgridValue[0] = ((ACM.cmsarea08.Controls.Itan_DataGrid)ctrl).SelectColumnField;
				return; // valori;
		} 
		if(ctrl.Controls.Count>0) {
			for(int i = 0; i<ctrl.Controls.Count;i++)
				CercaControllo(ctrl.Controls[i]);
		}
		return;
	}
	
		private void RicaricaDati(object o, EventArgs e) {
			ElencoGriglia=null;
			CaricaDati();
		}
		private void MentreCaricaDati(object o, DataGridItemEventArgs e) {
			//enable_me
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
				LinkButton lkb;
				lkb = (LinkButton)e.Item.FindControl("enable_me");
				if(lkb!=null) {
					string ena = ((DataRowView)e.Item.DataItem)["enabled"].ToString();
					lkb.CommandName="Abilita";
					lkb.CommandArgument=((DataRowView)e.Item.DataItem)["cultureinfo"].ToString();
					if(((DataRowView)e.Item.DataItem)["enabled"].ToString()=="1" && dclass.IsPredef("lingue","cultureinfo",lkb.CommandArgument))
						lkb.Attributes.Add("onclick","javascript: return confirm('" + rm.GetString("confirm_disable_predef") + "');");
					lkb.Text = 
						(ena=="1"? ("<img border=\"0\" class=\"jmsenabled1_sm\" src=\"../template/img/spacer.gif\" alt=\""+ rm.GetString("enabled") +"\" />"):
						 ("<img border=\"0\" class=\"jmsenabled0_sm\"  src=\"../template/img/disabled.gif\" alt=\""+ rm.GetString("disabled") +"\" />"))
						;
					
				}
			}
			
		}
		private void GrigliaCommand(object o, DataGridCommandEventArgs e) {
			//enable_me
			string culture="";
			if(e.CommandName.ToString() == "Abilita") {
				culture=e.CommandArgument.ToString();
				int reso = dclass.ExecuteScalar("update "+ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle+"lingue " +
				"set " +
				" enabled = if(enabled='1','0','1') " +				
				"where cultureinfo = '" +culture +"' limit 1;");
				if(reso>0) {
					ElencoGriglia=null;
					CaricaDati();
				} else {
					
				}
			}
		}
		
	}
	
}
